﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.SessionState;

namespace HttpLogger.Extensions
{
    public static class SpecialExtensions
    {
        public static string SpecializedToString(this ICollection nameCollection, string filter = "")
        {
            string ret = "";
            if (nameCollection == null) return ret; /*** EXIT ***/

            if (nameCollection is HttpModuleCollection)
            {
                var nvc = (HttpModuleCollection)nameCollection;
                ret = String.Join("\n", nvc.Cast<string>().Where(n => Regex.Match(n, filter).Success).Select(n => n + ":" + nvc[n]));
            }
            else if (nameCollection is HttpSessionState)
            {
                var nvc = (HttpSessionState)nameCollection;
                ret = String.Join("\n", nvc.Cast<string>().Where(n => Regex.Match(n, filter).Success).Select(n => n + ":" + nvc[n]));
            }
            else if (nameCollection is HttpCookieCollection)
            {
                var nvc = (HttpCookieCollection)nameCollection;
                ret = String.Join("\n", nvc.Cast<string>().Where(n => Regex.Match(n, filter).Success).Select(n => n + ":" + nvc[n].Values));
            }
            else if (nameCollection is NameValueCollection)
            {
                var nvc = (NameValueCollection)nameCollection;
                ret = String.Join("\n", nvc.Cast<string>().Where(n => Regex.Match(n, filter).Success).Select(n => n + ":" + nvc[n]));
            }
            else
            {
                ret = String.Join("\n", nameCollection.Cast<string>().Where(n => Regex.Match(n, filter).Success));
            }

            return ret;
        }
    }
}